package com.bjc.sdx.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;


import com.bjc.sdx.R;
import com.bjc.sdx.adapter.CustomerAdapter;
import com.bjc.sdx.model.ChildObj;
import com.bjc.sdx.model.ParentObj;

import java.util.ArrayList;

public class ExpandActivity extends Activity {
    private ExpandableListView expandableListView;
    private CustomerAdapter customerAdapter;
    private ArrayList<ParentObj> listData = new ArrayList<ParentObj>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expand_main);

        createGroupData();

        createChildData();

        expandableListView = (ExpandableListView) findViewById(R.id.expandable_list_view_id);
        customerAdapter = new CustomerAdapter(listData, this);
        expandableListView.setAdapter(customerAdapter);
        expandableListView.setGroupIndicator(null);

        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                for (int i = 0; i < customerAdapter.getGroupCount(); i ++) {
                    if (i != groupPosition) {
                        expandableListView.collapseGroup(i);
                    }
                }
            }
        });

        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(ExpandActivity.this, listData.get(groupPosition).getChilds().get(childPosition).getcName(), Toast.LENGTH_LONG).show();
                return true;
            }
        });
    }

    private void createGroupData() {
        ParentObj p1 = new ParentObj();
        p1.setpName("HP");
        listData.add(p1);

        ParentObj p2 = new ParentObj();
        p2.setpName("DELL");
        listData.add(p2);

        ParentObj p3 = new ParentObj();
        p3.setpName("Lenovo");
        listData.add(p3);

        ParentObj p4 = new ParentObj();
        p4.setpName("Sony");
        listData.add(p4);

        ParentObj p5 = new ParentObj();
        p5.setpName("HCL");
        listData.add(p5);

        ParentObj p6 = new ParentObj();
        p6.setpName("Samsung");
        listData.add(p6);
    }

    private void createChildData() {
        // preparing laptops collection(child)
        String[] hpModels = { "HP Pavilion G6-2014TX", "ProBook HP 4540", "HP Envy 4-1025TX" };
        String[] hclModels = { "HCL S2101", "HCL L2102", "HCL V2002" };
        String[] lenovoModels = { "IdeaPad Z Series", "Essential G Series", "ThinkPad X Series", "Ideapad Z Series" };
        String[] sonyModels = { "VAIO E Series", "VAIO Z Series", "VAIO S Series", "VAIO YB Series" };
        String[] dellModels = { "Inspiron", "Vostro", "XPS" };
        String[] samsungModels = { "NP Series", "Series 5", "SF Series" };

        for (ParentObj p : listData) {
            if (p.getpName().equals("HP")) {
                ArrayList<ChildObj> clists = new ArrayList<ChildObj>();
                for (int i = 0; i < hpModels.length; i ++) {
                    ChildObj cObj = new ChildObj();
                    cObj.setcName(hpModels[i]);
                    clists.add(cObj);
                }
                p.setChilds(clists);
            } else if (p.getpName().equals("DELL")) {
                ArrayList<ChildObj> clists = new ArrayList<ChildObj>();
                for (int i = 0; i < dellModels.length; i ++) {
                    ChildObj cObj = new ChildObj();
                    cObj.setcName(dellModels[i]);
                    clists.add(cObj);
                }
                p.setChilds(clists);
            } else if (p.getpName().equals("Lenovo")) {
                ArrayList<ChildObj> clists = new ArrayList<ChildObj>();
                for (int i = 0; i < lenovoModels.length; i ++) {
                    ChildObj cObj = new ChildObj();
                    cObj.setcName(lenovoModels[i]);
                    clists.add(cObj);
                }
                p.setChilds(clists);
            } else if (p.getpName().equals("Sony")) {
                ArrayList<ChildObj> clists = new ArrayList<ChildObj>();
                for (int i = 0; i < sonyModels.length; i ++) {
                    ChildObj cObj = new ChildObj();
                    cObj.setcName(sonyModels[i]);
                    clists.add(cObj);
                }
                p.setChilds(clists);
            } else if (p.getpName().equals("HCL")) {
                ArrayList<ChildObj> clists = new ArrayList<ChildObj>();
                for (int i = 0; i < hclModels.length; i ++) {
                    ChildObj cObj = new ChildObj();
                    cObj.setcName(hclModels[i]);
                    clists.add(cObj);
                }
                p.setChilds(clists);
            } else {
                ArrayList<ChildObj> clists = new ArrayList<ChildObj>();
                for (int i = 0; i < samsungModels.length; i ++) {
                    ChildObj cObj = new ChildObj();
                    cObj.setcName(samsungModels[i]);
                    clists.add(cObj);
                }
                p.setChilds(clists);
            }
        }
    }
}

